/**=========================================================
 * Controller: SystemMenuController
  =========================================================*/
App.controller('SystemMenuController', ['$scope', 'toaster', 'Utils', 'HttpUtils', 'DialogUtils', 'SimpleUiGrid',
    function($scope, toaster, utils, httpUtils, dialogUtils, simpleUiGridService) {
//===========================tree=================================
        var menuSelectChanged = function(menu){
            $scope.currentMenu = menu;
            searchUrlGrid();
            searchNamespaceGrid();
        }

        httpUtils.get('/sysMenu/getAllMenus',{},function(result){
             $scope.menuTreeOption = {
                name: "menuTree",
                setting: {
                    view: {
                        showLine: true,
                        selectedMulti: false
                    },
                    data: {
                        key: {
                            name: 'label'
                        }
                    },
                    callback: {
                        onClick: function(event, treeId, treeNode) {
                            $scope.$apply(function() {
                                menuSelectChanged(treeNode);
                            });
                        }
                    }
                },
                data: result.datas
             }
        })
        
        $scope.expandAll = function(){
            $scope.menuTree.expandAll(true);
        }
        
        $scope.collapseAll = function(){
            $scope.menuTree.expandAll(false);
        } 

        //新增子菜单
        $scope.addSubMenu = function() {
            if (utils.isEmpty($scope.currentMenu)){
                dialogUtils.alert('请先选中上级菜单');
            }else{
                var newMenu = $scope.menuTree.addNodes($scope.currentMenu, {
                    label: '新菜单',
                    root_id: $scope.currentMenu.root_id,
                    parent_id: $scope.currentMenu.tid,
                    status: '1',
                });
                
                $scope.menuTree.selectNode(newMenu[0]);
                menuSelectChanged(newMenu[0]);
            }
        };
        
        //新增根菜单
        $scope.addRootMenu = function() {
            var newMenu = $scope.menuTree.addNodes(null, {
                label: '新菜单',
                status: '1',
            });
            
            $scope.menuTree.selectNode(newMenu[0]);
            menuSelectChanged(newMenu[0]);
        };
        
        //刷新缓存
        $scope.refreshCache = function(){
             httpUtils.get('/sysMenu/refreshMenuUrlCache?refreshMenuUrlCache=true',{},
                function(result){
                    if (result.success){
                        toaster.pop('success', '', '刷新成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '刷新失败');
                }
            );
        }
        
        $scope.saveMenu = function(){
            httpUtils.post('/sysMenu/save',$scope.currentMenu,
                function(result){
                    if (result.success){
                        if (utils.isEmpty($scope.currentMenu.tid)){
                            $scope.currentMenu.tid = result.msg;
                        }
                        if (utils.isEmpty($scope.currentMenu.root_id)){
                            $scope.currentMenu.root_id = result.msg;
                        }
                        toaster.pop('success', '', '保存成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '保存失败');
                }
            );
        }
        
        $scope.deleteMenu = function(){
            if (utils.isEmpty($scope.currentMenu)){
                dialogUtils.alert('请先选中要删除的菜单')
            }else if (utils.isEmpty($scope.currentMenu.children)){ 
                dialogUtils.confirm('确定要删除吗?',
                    function (value) {
                        httpUtils.post('/sysMenu/delete',$scope.currentMenu,
                            function(result){
                                if (result.success){
                                    $scope.menuTree.removeNode($scope.currentMenu);
                                    $scope.currentMenu = null;
                                    urlGrid.clear();
                                    namespaceGrid.clear();
                                    toaster.pop('success', '', '删除成功');
                                }else{
                                    toaster.pop('error', '', result.msg);
                                }
                            },
                            function(result){
                                toaster.pop('error', '', '删除失败');
                            }
                        );
                    }
                );
            }else{    
                dialogUtils.alert('请先删除所有子节点');
            }
        }
//===========================namespaceGrid=================================            
        var namespaceGridColumns = [
            {
                displayName: 'Namespace',
                field: 'namespace'
            },
            {
                displayName: '状态',
                field: 'status',
                width: '80'
            },
            { 
                name: '', 
                field: 'widgets', 
                width: '100',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.saveMenuNamespace(row.entity)" title="保存" class="btn btn-xs btn-info">\
                            <em class="fa fa-save"></em>\
                        </button>\
                        <button ng-click="grid.appScope.deleteMenuNamespace(row.entity)" title="删除" class="btn btn-xs btn-danger">\
                            <em class="fa fa-trash"></em>\
                        </button>\
                    </div>\
                '
            }
        ];

        var searchNamespaceGrid = function() {
            if (utils.isEmpty($scope.currentMenu.tid)){
                namespaceGrid.clear();
            }else{
                var params = {
                    namespace: 'SystemMapper.getMenuNamespace',
                    menuID: $scope.currentMenu.tid
                }

                namespaceGrid.draw(params);
            }
        }; 

        var namespaceGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'namespaceGridOptions',
                pageOptionModel: 'namespaceGridPageOptions',
                gridApi: 'namespaceGridApi',
                pageType: 'common',
                needExport: false,
                columns: namespaceGridColumns,
                controllerSearchFn: searchNamespaceGrid,
                menus: [
                    {
                        title: '新增',
                        action: function ($event) {
                            if (!utils.isEmpty($scope.currentMenu)){
                                namespaceGrid.addRow({menu_id: $scope.currentMenu.tid, status: '1'},0);
                            }
                        }
                    }
                ]
            }
        );   
       
        $scope.saveMenuNamespace = function(data){
            httpUtils.post('/sysMenuNamespace/save',data,
                function(result){
                    if (result.success){
                        if (utils.isEmpty(data.tid)){
                            data.tid = result.msg;
                        }
                        toaster.pop('success', '', '保存成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '保存失败');
                }
            );
        };
        
        $scope.deleteMenuNamespace = function(data){
            dialogUtils.confirm('确定要删除吗?',
                function (value) {
                    httpUtils.post('/sysMenuNamespace/delete',data,
                        function(result){
                            if (result.success){
                                namespaceGrid.deleteRow(data);
                                toaster.pop('success', '', '删除成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '删除失败');
                        }
                    );
                }
            );
        };

//===========================urlGrid=================================        
        var urlGridColumns = [
            {
                displayName: 'Url',
                field: 'url'
            },
            {
                displayName: '状态',
                field: 'status',
                width: '80'
            },
            { 
                name: '', 
                field: 'widgets', 
                width: '100',
                enableCellEdit: false,
                cellTemplate: '\
                    <div class="ui-grid-cell-contents" style="text-align:center;">\
                        <button ng-click="grid.appScope.saveMenuUrl(row.entity)" title="保存" class="btn btn-xs btn-info">\
                            <em class="fa fa-save"></em>\
                        </button>\
                        <button ng-click="grid.appScope.deleteMenuUrl(row.entity)" title="删除" class="btn btn-xs btn-danger">\
                            <em class="fa fa-trash"></em>\
                        </button>\
                    </div>\
                '
            }
        ];
            
        var searchUrlGrid = function() {
            if (utils.isEmpty($scope.currentMenu.tid)){
                urlGrid.clear();
            }else {
                var params = {
                    namespace: 'SystemMapper.getMenuUrl',
                    menuID: $scope.currentMenu.tid
                }

                urlGrid.draw(params);
            }
        }; 

        var urlGrid = simpleUiGridService.init($scope, 
            {
                optionModel: 'urlGridOptions',
                pageOptionModel: 'urlGridPageOptions',
                gridApi: 'urlGridApi',
                pageType: 'common',
                needExport: false,
                columns: urlGridColumns,
                controllerSearchFn: searchUrlGrid,
                menus: [
                    {
                        title: '新增',
                        action: function ($event) {
                            if (!utils.isEmpty($scope.currentMenu)){
                                urlGrid.addRow({menu_id: $scope.currentMenu.tid, status: '1'},0);
                            }
                        }
                    }
                ]
            }
        );  
        
        $scope.saveMenuUrl = function(data){
            httpUtils.post('/sysMenuUrl/save',data,
                function(result){
                    if (result.success){
                        if (utils.isEmpty(data.tid)){
                            data.tid = result.msg;
                        }
                        toaster.pop('success', '', '保存成功');
                    }else{
                        toaster.pop('error', '', result.msg);
                    }
                },
                function(){
                    toaster.pop('error', '', '保存失败');
                }
            );
        };
        
        $scope.deleteMenuUrl = function(data){
            dialogUtils.confirm('确定要删除吗?',
                function (value) {
                    httpUtils.post('/sysMenuUrl/delete',data,
                        function(result){
                            if (result.success){
                                urlGrid.deleteRow(data);
                                toaster.pop('success', '', '删除成功');
                            }else{
                                toaster.pop('error', '', result.msg);
                            }
                        },
                        function(result){
                            toaster.pop('error', '', '删除失败');
                        }
                    );
                }
            );
        };       
    }
]);
